<template>
  <el-card class="section-card">
    <template #header>
      <div class="section-header">
        <h3>理财产品</h3>
        <div class="filter-controls">
          <el-select :model-value="filters.riskLevel" placeholder="风险等级" clearable @change="(val) => onFilterChange('riskLevel', val)">
            <el-option label="低风险" :value="1"></el-option>
            <el-option label="中低风险" :value="2"></el-option>
            <el-option label="中等风险" :value="3"></el-option>
            <el-option label="中高风险" :value="4"></el-option>
            <el-option label="高风险" :value="5"></el-option>
          </el-select>
          <el-select :model-value="filters.duration" placeholder="投资期限" clearable @change="(val) => onFilterChange('duration', val)">
            <el-option label="30天以内" :value="{ min: 0, max: 30 }"></el-option>
            <el-option label="30-90天" :value="{ min: 30, max: 90 }"></el-option>
            <el-option label="90-180天" :value="{ min: 90, max: 180 }"></el-option>
            <el-option label="180-365天" :value="{ min: 180, max: 365 }"></el-option>
            <el-option label="365天以上" :value="{ min: 365, max: 99999 }"></el-option>
          </el-select>
          <el-button :icon="Refresh" @click="onRefresh" :loading="loading">刷新</el-button>
        </div>
      </div>
    </template>

    <div class="products-list" v-loading="loading">
      <div class="product-item" v-for="product in products" :key="product.id">
        <div class="product-main">
          <div class="product-info">
            <div class="product-title">{{ product.name }}</div>
            <div class="product-tags">
              <el-tag :type="getRiskTagType(product.riskLevel)" size="small">
                {{ getRiskLevelText(product.riskLevel) }}
              </el-tag>
              <el-tag size="small">{{ product.duration }}天</el-tag>
              <el-tag v-if="product.isHot" type="danger" size="small">热销</el-tag>
            </div>
            <div class="product-desc">{{ product.description }}</div>
          </div>
          <div class="product-returns">
            <div class="return-rate">
              <span class="rate-label">预期年化收益率</span>
              <span class="rate-value">{{ formatRate(product.expectedReturn) }}%</span>
            </div>
            <div class="min-amount">
              起购金额：¥{{ formatAmount(product.minAmount) }}
            </div>
          </div>
          <div class="product-actions">
            <el-button size="small" @click="onProductDetail(product)">查看详情</el-button>
            <el-button type="primary" size="small" @click="onPurchase(product)" 
                       :disabled="product.status !== 1">
              {{ product.status === 1 ? '立即购买' : '暂停销售' }}
            </el-button>
          </div>
        </div>
        <div class="product-progress" v-if="product.salesProgress">
          <div class="progress-label">
            <span>销售进度</span>
            <span>{{ formatAmount(product.salesAmount) }} / {{ formatAmount(product.targetAmount) }}</span>
          </div>
          <el-progress :percentage="product.salesProgress" :show-text="false" :stroke-width="4"></el-progress>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
        :current-page="pagination.page"
        :page-size="pagination.size"
        :page-sizes="[10, 20, 50]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next"
        @current-change="onPageChange"
        @size-change="onSizeChange"
        />
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { Refresh } from '@element-plus/icons-vue';
import { formatAmount, formatRate, getRiskLevelText, getRiskTagType } from './utils';

defineProps({
  products: {
    type: Array,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  },
  filters: {
    type: Object,
    required: true
  },
  pagination: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['refresh', 'filter-change', 'page-change', 'size-change', 'product-detail', 'purchase']);

const onRefresh = () => {
  emit('refresh');
};

const onFilterChange = (key, value) => {
  emit('filter-change', { key, value });
};

const onPageChange = (page) => {
  emit('page-change', page);
};

const onSizeChange = (size) => {
  emit('size-change', size);
};

const onProductDetail = (product) => {
  emit('product-detail', product);
};

const onPurchase = (product) => {
  emit('purchase', product);
};
</script>

<style scoped>
.section-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

/* 筛选控件 */
.filter-controls {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* 产品列表样式 */
.products-list {
  min-height: 400px;
}

.product-item {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
  background-color: white;
  transition: all 0.3s ease;
}

.product-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
}

.product-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.product-info {
  flex: 1;
}

.product-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
}

.product-tags {
  margin-bottom: 8px;
}

.product-tags .el-tag {
  margin-right: 8px;
}

.product-desc {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

.product-returns {
  text-align: center;
  margin: 0 20px;
}

.return-rate {
  margin-bottom: 8px;
}

.rate-label {
  display: block;
  font-size: 12px;
  color: #909399;
  margin-bottom: 4px;
}

.rate-value {
  font-size: 28px;
  font-weight: 700;
  color: #e6a23c;
}

.min-amount {
  font-size: 12px;
  color: #909399;
}

.product-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.product-progress {
  border-top: 1px solid #ebeef5;
  padding-top: 12px;
}

.progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-main {
    flex-direction: column;
    align-items: stretch;
  }
  
  .product-returns {
    margin: 16px 0;
    text-align: left;
  }
  
  .product-actions {
    flex-direction: row;
  }
  
  .filter-controls {
    flex-direction: column;
    align-items: stretch;
  }
}
</style>
